class ModeTagLib {

    def selectMode = { attrs ->
        def currentMode = attrs.get('currentMode')
        def projectMode = attrs.get('projectMode')

        String str = "<select id=\"mode\" name=\"mode\">"
            str += "<option"
            if(projectMode==0) str += " selected"
            str += " value=\"0\">Budget only</option>"
            str += "<option"
            if(projectMode==1) str += " selected"
            str += " value=\"1\">Sandbox only</option>"
            str += "<option"
            if(projectMode==2) str += " selected"
            str += " value=\"2\">Budget and Sandbox</option>"
        out << str + "</select>"
    }

    def btnMode = { attrs ->
        def currentMode = attrs.get('mode')
        def currentView = attrs.get('currentView')
        def currentController = attrs.get('currentController')
        def idProject = attrs.get('idProject')
        String str=""

        str += "<div class=\"mode\">"

        // Allow budget mode only between november and december
        Date d = new Date() // get the current date
        d.year=0

        //TODO: get this information from database
       Date startDate = new Date('11/01/1900')
       Date endDate = new Date('12/31/1900 23:59:00')

       // Check if the budget mode is allowed according to the current date
        if((d>=startDate && d<=endDate)){
            str+="<form action=\"/MegaSoft/mode/changeMode\" method=\"post\" >"

            if(currentMode != "budget"){ // if in sandbox mode, allow button budget mode
                str+= "<span style=\"font-weight:bold; padding-right:10px\">Sandbox mode</span>"
                str += "<span class=\"buttons\">"
                str += "<input type=\"hidden\" id=\"currentView\" name=\"currentView\" value=\""+currentView+"\"/>"
                str += "<input type=\"hidden\" id=\"currentController\" name=\"currentController\" value=\""+currentController+"\"/>"
                str += "<input type=\"hidden\" id=\"idProject\" name=\"idProject\" value=\""+idProject+"\"/>"
                str += "<input type=\"submit\" class=\"change_mode\" value=\"Switch to budget mode\" name=\"change_mode\"/>"
                str+= "</span>"
            }
            else{ // else if in budget mode, allow sandbox button
                str += "<span class=\"buttons\">"
                str += "<input type=\"hidden\" id=\"currentView\" name=\"currentView\" value=\""+currentView+"\"/>"
                str += "<input type=\"hidden\" id=\"currentController\" name=\"currentController\" value=\""+currentController+"\"/>"
                str += "<input type=\"hidden\" id=\"idProject\" name=\"idProject\" value=\""+idProject+"\"/>"
                str += "<input type=\"submit\" class=\"change_mode\" value=\"Switch to sandBox mode\" name=\"change_mode\"/>"
                str+= "</span>"
                str+= "<span style=\"font-weight:bold; padding-left:10px\">Budget mode</span>"
            }

            str += "</form>"

            if(currentMode != "budget"){ // if in sandbox mode, allow button budget mode
                str += "<form action=\"/MegaSoft/mode/save\" method=\"post\" >"
                str += "<input type=\"submit\" class=\"change_mode\" value=\"save\" name=\"save\"/>"
                str += "</form>"
                str += "<form action=\"/MegaSoft/mode/rollback\" method=\"post\" >"
                str += "<input type=\"submit\" class=\"change_mode\" value=\"undo\" name=\"undo\"/>"
                str += "</form>"
            }
            
        }
        else{ // if the current month is before november

            str+= "<span style=\"font-weight:bold; padding-right:10px\">Sandbox mode only</span>"

        }
        //str += currentMode
        str += "</div>"

        out << str
    }
}
